package com.yand.controller.system;


import com.github.pagehelper.PageInfo;
import com.yand.api.annotation.SysLogger;
import com.yand.api.entity.common.Result;
import com.yand.api.entity.system.TSysRole;
import com.yand.api.entity.system.TSysUser;
import com.yand.api.service.system.RoleService;
import com.yand.api.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@Api(tags = "用户操作相关接口")
@RestController
@RequestMapping("system")
public class UserController {


    @Autowired
    private UserService userService;


    @Autowired
    private RoleService roleService;


    /**
     * 跳转用户列表
     * @param user
     * @param model
     * @return
     */
    @ApiOperation("跳转用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户对象", required = true)
    })
    @GetMapping("/user")
    public ModelAndView toUserPage(TSysUser user, Model model){

        ModelAndView modelAndView = new  ModelAndView();

        modelAndView.setViewName("system/user/user-list");

        PageInfo<TSysUser> userPageInfo = userService.queryUserList(user);

        model.addAttribute("pageInfo",userPageInfo);

        model.addAttribute("user",user);

        return modelAndView;
    }


    @ApiOperation("跳转添加/修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    })
    @GetMapping("/user/{userId}")
    public ModelAndView toEditUserPage(@PathVariable(value = "userId") Integer userId, Model model){
        ModelAndView modelAndView = new ModelAndView();
        String typeSuffix = (userId == 0) ? "add":"edit";
        modelAndView.setViewName("system/user/user-"+typeSuffix);
        TSysUser user = userService.getUserById(userId);
        //获取所有角色信息
        TSysRole role = new TSysRole();
        role.setPageSize(999999999);
        PageInfo<TSysRole> rolePageInfo = roleService.queryRoleList(role);
        model.addAttribute("user",user);
        model.addAttribute("roleList",rolePageInfo.getList());
        return modelAndView;
    }

    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "user对象", required = true)
    })
    @PostMapping("/user")
    public Result addUser(TSysUser user){

        userService.insertUser(user);

        return new Result(0);
    }

    @ApiOperation("修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "user对象", required = true)
    })
    @PutMapping("/user")
    public Result updateUser(TSysUser user){
        userService.updateUser(user);

        return new Result(0);
    }


    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true)
    })
    @DeleteMapping("/user/{userId}")
    public Result deleteUser(@PathVariable(value = "userId") Integer userId){

        userService.deleteUser(userId);

        return new Result(0);
    }


    @ApiOperation("跳转修改密码页面")
    @GetMapping("/user/password")
    public ModelAndView toUpdatePwdPage(){

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("system/user/user-pwd");

        return modelAndView;
    }

    @ApiOperation("修改密码")
    @PostMapping("/user/password")
    public Result updatePwd(String oldPassword, String newPassword){

        return userService.updatePwd(oldPassword, newPassword);
    }










}
